package com.uin.saas;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * 代码生成器
 *
 * @author BAIYEQIYA
 * @date 2018/5/11 11:58
 * @since JDK 1.8
 */
public class GeneratorServiceEntity {

    private static DbType dbType = DbType.MYSQL;
    private static String dbUrl =
            "jdbc:mysql://127.0.0.1:3306/master_tenant?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT"
                    + "&nullNamePatternMatchesAll=true";
    //        private static String userName = "kuangzhuandzt";
    //        private static String password = "houduan";
    private static String userName = "root";
    private static String password = "root";
    private static String driverName = "com.mysql.jdbc.Driver";

    private static String author = System.getProperty("user.name");
    private static String outputDir = System.getProperty("user.dir") + "\\src\\main\\java";

    private static String packageName = "com.uin.saas";

    private static String[] tableNames = {"master_tenant","user"};
    private static MySqlTypeConvert mySqlTypeConvert = new MySqlTypeConvert() {
        @Override
        public DbColumnType processTypeConvert(String fieldType) {
            System.out.println("字段数据类型----------" + fieldType);
            fieldType = fieldType.toLowerCase();
            if (fieldType.contains("bigint")) {
                return DbColumnType.BIG_INTEGER;
            }
            if (fieldType.contains("datetime")) {
                return DbColumnType.TIMESTAMP;
            }
            if (fieldType.contains("tinyint")) {
                return DbColumnType.BOOLEAN;
            }
            return super.processTypeConvert(fieldType);
        }
    };

    public static void main(String[] args) {
        generateCode();
    }

    private static void generateCode() {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(dbType).setUrl(dbUrl).setUsername(userName).setPassword(password)
                .setDriverName(driverName);

        //自定义数据库与 java 数据对应类型
        dataSourceConfig.setTypeConvert(mySqlTypeConvert);

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true).setEntityLombokModel(true).setDbColumnUnderline(true)
                .setNaming(NamingStrategy.underline_to_camel).setColumnNaming(NamingStrategy.underline_to_camel)
                //修改替换成你需要的表名，多个表名传数组
                .setInclude(tableNames).setRestControllerStyle(true);

//        strategyConfig.setSuperEntityClass("com.hciot.common.base.entity.BaseEntity")
//                .setSuperMapperClass("com.hciot.common.base.dao.BaseDao")
//                .setSuperServiceClass("com.hciot.common.base.service.BaseService")
//                .setSuperServiceImplClass("com.hciot.common.base.service.impl.BaseServiceImpl");

        strategyConfig.setFieldPrefix(new String[]{"is_"});

        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(false).setAuthor("WYY-SSS").setOutputDir(outputDir).setFileOverride(true);
        config.setMapperName("%sDao");
        config.setServiceName("%sService");
        config.setBaseResultMap(true).setBaseColumnList(true).setEnableCache(false);

        new AutoGenerator().setGlobalConfig(config).setDataSource(dataSourceConfig).setStrategy(strategyConfig)
                .setPackageInfo(new PackageConfig().setParent(packageName).setController("controller").setEntity(
                        "entity").setXml("dao")
                        .setMapper("dao")).execute();
    }
}
